package com.eshop.dao.Impl;

import com.eshop.dao.OrderDao;
import com.eshop.model.Order;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public Order queryOrderByOrderId(int orderId) {
        String sql = "select * from t_order where order_id=?";
        return queryForOne(Order.class,sql,orderId);
    }

    @Override
    public Order queryNewOrderByUserId(int userId) {
        String sql = "select * from t_order where order_id = (select max(order_id) from t_order where user_id = ?)";
        return queryForOne(Order.class,sql,userId);
    }

    @Override
    public List<Order> queryOrderByUserId(int userId) {
        String sql = "select * from t_order where user_id=?";
        return queryForList(Order.class,sql,userId);
    }

    @Override
    public List<Order> queryOrderByStatusAndUserId(int status, int userId) {
        String sql = "select * from t_order where order_status=? and user_id=?";
        return queryForList(Order.class,sql,status,userId);
    }

    @Override
    public void addOrder(Order order) {
        String sql = "INSERT INTO t_order(`order_code`, `user_id`, `order_status`, `order_address`, `order_postalfee`, `order_date`) VALUES (?,?,?,?,?,?)";
        update(sql,order.getOrder_code(),order.getUser_id(),order.getOrder_status(),order.getOrder_address(),order.getOrder_postalfee(),order.getOrder_date());
    }

    @Override
    public void updateOrderStatus(Order order) {
        String sql = "UPDATE t_order SET `order_status` = ? WHERE `order_id` = ?";
        update(sql,order.getOrder_status(),order.getOrder_id());
    }
}
